//
//  SFCenterAnimationPresentObject.swift
//  AlertViewController
//
//  Created by SmartStrategy on 2019/4/17.
//  Copyright © 2019 SmartStrategy. All rights reserved.
//

import UIKit

class SFCenterAnimationPresentObject: NSObject,UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.3
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let tovc:UIViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
        let fromvc = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
        let containerView = transitionContext.containerView
        let duration = self.transitionDuration(using: transitionContext)
        
        if tovc.isBeingPresented {
            if tovc.isKind(of: ZCDateViewController.self){
                let tovc1 = tovc as! ZCDateViewController
                containerView.addSubview(tovc.view)
                tovc.view.frame = CGRect.init(x: 0, y: 0, width: containerView.frame.size.width, height: containerView.frame.size.height)
                tovc1.contentView?.frame = CGRect.init(x: 0, y: kScreenHeight, width: kScreenWidth, height: 225)
                UIView.animate(withDuration: duration, animations: {
                    tovc1.contentView?.frame = CGRect.init(x: 0, y: kScreenHeight-225, width: kScreenWidth, height: 225)
                }) { (finished:Bool) in
                    transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
                }
            }
        }else if (fromvc!.isBeingDismissed){
            if fromvc!.isKind(of: ZCDateViewController.self){
                let fromvc1 = fromvc as! ZCDateViewController
                fromvc?.view.alpha = 1.0
                
                UIView.animate(withDuration: duration, animations: {
                    fromvc1.contentView?.frame = CGRect.init(x: 0, y: kScreenHeight, width: kScreenWidth, height: 225)
                    fromvc!.view.alpha = 0.0;
                }) { (finish:Bool) in
                    transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
                }
            }
            
        }
        
        
    }
    

}
